import {Injectable} from '@angular/core';
import {CanActivate, ActivatedRoute} from '@angular/router';
import {AuthenticationHelper} from '../app.authentication';
import {UserService} from '../services/app.user';

@Injectable()
export class AdminGuard implements CanActivate {

    constructor(private authService:AuthenticationHelper, private userService:UserService, private route: ActivatedRoute) {
    }

    canActivate():boolean {               
        if (!this.authService.isAdmin()) {
            if (this.authService.hasUser()) {
                this.authService.redirectToDashboard();
                return false;
            } else {
            //    this.userService.getuserInfo().subscribe(
            //         data => {
            //              return this.isAdmin(data) 
            //             },
            //         error => {
            //             return false
            //         });
            return true;
            }

        } else {
            return true;
        }

    }

    isAdmin(user) {
        this.authService.setUser(user);     
        if (!this.authService.isAdmin()) {
            this.authService.redirectToDashboard();
            return false;
        } else {
            return true;
        }
    }
    

}
